package com.huobao.yunying.activitys_hehuoren;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Huiyaun_List;
import com.huobao.yunying.entity.HuiyuanList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 会员列表
 * @author Administrator
 *
 */
public class Act_Huiyuan_List extends Act_TabBase implements OnScrollListener{

	@ViewInject(R.id.listview)
	private ListView listview;
	private View HeadView;
	private View FootView;
	private TextView tv_search;
	private TextView tv_foot_msg;
	private Adapter_Huiyaun_List adapter;
	private int m_CurrentPage = 1;
	private boolean isLoading = true;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_huiyuan_list;
	}

	@Override
	protected String getTitleText() {
		return "会员详情";
	}

	@Override
	protected void init() {
		super.init();
		HeadView = LayoutInflater.from(context).inflate(R.layout.item_huiyaun_list_title, null);
		check(HeadView);
		FootView = LayoutInflater.from(context).inflate(R.layout.item_footview, null);
		tv_search = (TextView) HeadView.findViewById(R.id.edt_search);
		tv_foot_msg =(TextView) FootView.findViewById(R.id.tv_message);
		listview.addHeaderView(HeadView);
		listview.addFooterView(FootView);
		adapter = new Adapter_Huiyaun_List(context);
		listview.setAdapter(adapter);
		listview.setOnScrollListener(this);
		getList(true);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if(isLoading)return;
		if((firstVisibleItem + visibleItemCount) == totalItemCount){
			m_CurrentPage ++;
			getList(false);
		}
	}

	private void getList(boolean isShowLoading){
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams("app","partner","act","member","page",""+m_CurrentPage);
		Http.getInstant().sendRequest(context, isShowLoading, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<HuiyuanList> list = JSON.parseArray(jsonObject.getString("retval"), HuiyuanList.class);
				if(list == null || list.size() == 0){
					tv_foot_msg.setText("加载完成");
					isLoading = true;
					return;
				}
				adapter.addAll(list);
				isLoading = false;
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
		
	}
}
